package hhm.framessm.portal.dwr;

import java.util.Collection;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;

public class DwrTest {

	public void send(final String content) {

		System.out.println(content);
		Runnable run = new Runnable() {
			private ScriptBuffer script = new ScriptBuffer();

			public void run() {
				// 设置要调用的 js及参数
				script.appendCall("show", content);
				// 得到所有ScriptSession
				Collection<ScriptSession> sessions = Browser.getTargetSessions();
				// 遍历每一个ScriptSession
				for (ScriptSession scriptSession : sessions) {
					scriptSession.addScript(script);
				}
			}
		};
		// 执行推送
		Browser.withAllSessions(run);
	}

	public void test(String message) {
		WebContext contex = WebContextFactory.get();

		// 获取client.jsp页面的ScriptSession集合，注意："/dwrpush/client.jsp"

		Collection<ScriptSession> sessions = contex.getAllScriptSessions();

		System.out.println(sessions);
		System.out.println(message);
		Util util = new Util(sessions);

		ScriptBuffer sb = new ScriptBuffer();

		sb.appendScript("showMessage(");

		sb.appendData(message);

		sb.appendScript(")");

		util.addScript(sb);
	}
}
